package com.wei.controller;


import com.wei.entity.GdElectricityfee;
import com.wei.entity.GdWaterfee;
import com.wei.mapper.GdElectricityfeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdElectricityfeeController {

    @Autowired
    private GdElectricityfeeMapper electricityfeeMapper;

    @RequestMapping("/getElectricityfees")
    public List<GdElectricityfee> getElectricityfee(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        return electricityfeeMapper.selectByMap(map);
    }

    @RequestMapping("/addElectricityfee")
    public void addElectricityfee(@RequestParam Map<String,Object> electricityfeeObj){
        GdElectricityfee electricityfee =setParam(electricityfeeObj);
        electricityfeeMapper.insert(electricityfee);
    }

    @RequestMapping("/updateElectricityfee")
    public void updateElectricityfee(@RequestParam Map<String,Object> electricityfeeObj){
        GdElectricityfee electricityfee = setParam(electricityfeeObj);
        electricityfeeMapper.updateById(electricityfee);
    }

    @RequestMapping("/deleteElectricityfee")
    public void deleteElectricityfee(@RequestParam String electricityfeeId){
        electricityfeeMapper.deleteById(electricityfeeId);
    }

    /**
     * 通过房产地址、月份获取电费记录，用于月度账单的保存生成(管理员)
     * @param paramObj
     * @return
     */
    @RequestMapping("/getElectricityfee")
    public Double hasElectricityfee(@RequestParam Map<String,Object> paramObj){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("electricityfee_date", paramObj.get("feeDate"));
        map.put("property_address", paramObj.get("propertyAddress"));
        return electricityfeeMapper.selectByMap(map).get(0).getElectricityfeeTotalfee();
    }

    /**
     * 通过房产地址获取电费记录，用于电费的查询(普通用户)
     * @param address
     * @return
     */
    @RequestMapping("/getElectricityfeeByAddress")
    public List<GdElectricityfee> getElectricityfeeByAddress(@RequestParam String address){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("property_address", address);
        return electricityfeeMapper.selectByMap(map);
    }

    /**
     * 设置参数方法
     * @param electricityfeeObj
     * @return
     */
    private GdElectricityfee setParam(@RequestParam Map<String, Object> electricityfeeObj) {
        GdElectricityfee electricityfee=new GdElectricityfee();
        electricityfee.setElectricityfeeId((String) electricityfeeObj.get("electricityfeeId"));
        electricityfee.setElectricityfeeLreading(Integer.parseInt(electricityfeeObj.get("electricityfeeLreading").toString()));
        electricityfee.setElectricityfeeTreading(Integer.parseInt( electricityfeeObj.get("electricityfeeTreading").toString()));
        electricityfee.setElectricityfeeAmount(Integer.parseInt(electricityfeeObj.get("electricityfeeAmount").toString()));
        electricityfee.setElectricityfeePrice(Double.parseDouble(electricityfeeObj.get("electricityfeePrice").toString()));
        electricityfee.setElectricityfeeTotalfee(Double.parseDouble( electricityfeeObj.get("electricityfeeTotalfee").toString()));
        electricityfee.setElectricityfeeDate((String) electricityfeeObj.get("electricityfeeDate"));
        electricityfee.setElectricityfeeForeignkey((String) electricityfeeObj.get("electricityfeeForeignkey"));
        electricityfee.setPropertyAddress((String) electricityfeeObj.get("propertyAddress"));
        return electricityfee;
    }
}

